package ph.edu.ics.sp.graphisched.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.jdesktop.application.Application;

import ph.edu.ics.sp.graphisched.GSCFilter;
import ph.edu.ics.sp.graphisched.GraphiSchedConstants;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class OpenExistingFilePanel extends javax.swing.JPanel implements
		GraphiSchedConstants {
	private JFrame frame;
	private JLabel heading;
	private JButton browseButton;
	private JTextField filePath;
	private JButton backButton;
	private JButton loadData;
	private JFileChooser fileChooser;
	private JLabel selectFileLabel;

	public OpenExistingFilePanel(JFrame frame) {
		super();
		this.frame = frame;
		initGUI();
	}

	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(352, 222));
			this.setLayout(null);
			{
				heading = new JLabel();
				this.add(heading);
				heading.setName("heading");
				heading.setBounds(59, 14, 226, 42);
			}
			{
				selectFileLabel = new JLabel();
				this.add(selectFileLabel);
				selectFileLabel.setBounds(29, 85, 136, 18);
				selectFileLabel.setName("selectFileLabel");
			}
			{
				browseButton = new JButton();
				this.add(browseButton);
				browseButton.setName("browseButton");
				browseButton.setBounds(214, 112, 99, 21);
				browseButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						browseButtonActionPerformed(evt);
					}
				});
			}
			{
				filePath = new JTextField();
				this.add(filePath);
				filePath.setBounds(34, 112, 174, 21);
			}
			{
				backButton = new JButton();
				this.add(backButton);
				backButton.setName("backButton");
				backButton.setBounds(71, 161, 93, 21);
				backButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						backButtonActionPerformed(evt);
					}
				});
			}
			{
				loadData = new JButton();
				this.add(loadData);
				loadData.setName("loadData");
				loadData.setBounds(169, 161, 116, 21);
				loadData.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						loadFileActionPerformed(evt);
					}
				});
			}
			{
				getFileChooser();
			}
			Application.getInstance().getContext().getResourceMap(getClass())
					.injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JFileChooser getFileChooser() {
		GSCFilter filter = new GSCFilter();

		if (fileChooser == null) {
			fileChooser = new JFileChooser();
		}

		fileChooser.setFileFilter(filter);

		return fileChooser;
	}

	private void backButtonActionPerformed(ActionEvent evt) {
		frame.setContentPane(new GraphiSchedIndexPanel(frame));
		frame.setVisible(true);
		frame.setResizable(false);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.repaint();
	}

	private void loadFileActionPerformed(ActionEvent evt) {

		if (filePath.getText().length() != 0) {
			// sets the frame properties and renders the GraphiSchedTablePanel
			File file = new File(filePath.getText());
			@SuppressWarnings("unused")
			FileInputStream fis = null;
			try{
				fis = new FileInputStream(file);
				frame.setVisible(false);
				frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
				frame.setLocationRelativeTo(null);
				frame.setContentPane(new GraphiSchedTablePanel(frame, file));
				frame.setVisible(true);
				frame.repaint();
			}catch(FileNotFoundException e) {
				JOptionPane.showMessageDialog(null, "File not Found!", "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}

	}

	private void browseButtonActionPerformed(ActionEvent evt) {
		fileChooser.showOpenDialog(frame);
		filePath.setText(null);
		String path = null;
		try {
			path = fileChooser.getSelectedFile().getAbsolutePath();
		} catch (NullPointerException e) {
		}
		if(path != null){
			filePath.setText(path);	
		}
	}

}
